<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/smarty/plugins/function.module.php, $Author:  $ */

function smarty_function_module($params, &$smarty)
{
   App::setParam('tpl_processing', 0);
   static $aModules = array();  //module results cache

   if (! isset($params['module']))
   {
       $smarty->trigger_error('{module} - missed "module" param', E_USER_ERROR, __FILE__, __LINE__);
   }

   if (! isset($params['component']))
   {
       $smarty->trigger_error('{module} - missed "component" param', E_USER_ERROR, __FILE__, __LINE__);
   }

   $sModule    = $params['module'];
   $sComponent = $params['component'];
   $sBlock     = isset($params['block']) ? $params['block'] : '';

   unset($params['module']);
   unset($params['component']);
   unset($params['block']);

   $sHash = $sModule.'::'.$sComponent.'::'.md5(serialize($params));

   //if results not cached yet
   if (!isset($aModules[$sHash]))
   {
       $oComp = App::getModuleComponent($sModule, $sComponent, $params);
       //store results to cache
       $aModules[$sHash] = $oComp ? $oComp->process() : '';
   }
   
   //return result
   if ($sBlock) //get by block name
   {
       $srcHtml = $aModules[$sHash][$sBlock];
   }
   else //get deafult block
   {
       if (is_array($aModules[$sHash]))
       {
           reset($aModules[$sHash]);
           $srcHtml = current($aModules[$sHash]);
       }
       else
           $srcHtml = $aModules[$sHash];
   }
   App::setParam('tpl_processing', 1);
   
   $aParts = explode('::', $sHash);
   if (!defined('PHPFOX_NO_PLUGINS') && App::getParam('bHooks') && (isset($aParts[0]) && isset($aParts[1])))
   {
   		$sGetModule = $aParts[0];
   		$sGetComponent = $aParts[1];
   }
   
   if (isset($sGetModule) && isset($sGetComponent))
   {
       	(($sHook = App::getHook($sModule . $sComponent . 'Start')) ? eval($sHook) : false);
      	echo $srcHtml;
       	(($sHook = App::getHook($sModule . $sComponent . 'End')) ? eval($sHook) : false);
   }
   else 
   {   
   		return $srcHtml;
   }
}
?>